#	usage: python build_gallery.py <dir_of_images>
import sys,os
assert len(sys.argv)==2, "directory name required"
f=file(os.path.join(sys.argv[1],"index.html"),"w")
header="""
<html>
	<head>
		<script>
			var start=0;
			var speed=2;
			function scrollDown(){
				if(start==1){
					window.scrollBy(0,speed);
					setTimeout('scrollDown()',50);
				}
			}
			function keyPressed(event){
				if(event.keyCode==49){
					speed++;
				}else if(event.keyCode==50){
					speed--;
				}else{
					start=1-start;
					if(start==1)scrollDown();
				}
			}
		</script>
	</head>
<body onkeypress='keyPressed(event)'> 
"""
def isImg(s):
	return s.endswith("jpg") or s.endswith("gif") or s.endswith("png")

f.write(header)
l=[s for s in os.listdir(sys.argv[1]) if isImg(s)]

l.sort(key=lambda a:int(os.path.splitext(a)[0]))
for s in l:
	f.write("<img src='%s'>\n" % s)
f.write("</body></html>\n")
f.close()
